<template>
<div>
    <Modal title="Image" v-model="visible">
        <img :src="imageSrc" v-if="visible" style="width: 100%">
    </Modal>
    <RadioGroup v-model="paging.status" type="button" @on-change="fRadio">
        <Radio label=-1>全部</Radio>
        <Radio label=0>未认证</Radio>
        <Radio label=1>认证成功</Radio>
        <Radio label=2>认证驳回</Radio>
    </RadioGroup>
    <Table border :columns="columns" :data="data"></Table>
    <Page :total="paging.total" @on-change="changepage" :page-size="paging.size"></Page>
</div>
</template>
<script>
import util from '../libs/util';
    export default {

        created() {
            this.initFormatter(); 
            this.paging = {
                page: 1,
                size: 8,
                status: -1,
            };
            this.load_data(this.paging);
            this.columns = [
                    {
                        title: 'ID',
                        key: 'id',
                    },
                    {
                        title: '昵称',
                        key: 'nickname',
                        render: function (h, params) {
                            var value = params.row.nickname;
                            if (!params.row.nickname) {
                                value = '尚未填写';
                            }
                            return h('div', 
                              value
                            );
                        }
                    },
                    {
                        title: '姓名',
                        key: 'name',
                        render: function (h, params) {
                            var value = params.row.name;
                            if (!params.row.name) {
                                value = '尚未填写';
                            }
                            return h('div', 
                              value
                            );
                        }
                    },
                    {
                        title: '学校',
                        key: 'school_name',
                         render: function (h, params) {
                            var value = params.row.school_name;
                            if (!params.row.school_name) {
                                value = '尚未填写';
                            }
                            return h('div', 
                              value
                            );
                        }
                    },
                    {
                        title: '学院',
                        key: 'academy',
                        render: function (h, params) {
                            var value = params.row.academy;
                            if (!params.row.academy) {
                                value = '尚未填写';
                            }
                            return h('div', 
                              value
                            );
                        }
                    },
                    {
                        title: '寝室',
                        key: 'dormitory',
                        render: function (h, params) {
                            var value = params.row.dormitory;
                            if (!params.row.dormitory) {
                                value = '尚未填写';
                            }
                            return h('div', 
                              value
                            );
                        }
                    },
                    {
                        title: '学生证正面照',
                        key: 'front_url',
                        render: (h, params) => {
                           var pic = params.row.front_url;
                           if (!pic) {
                               return h('div', 
                                    '尚未上传'
                               );
                           }
                            return h("img",{
                                    attrs: {
                                        src: pic,
                                    },
                                    style:{
                                        width:"px",
                                        height:"45px",
                                    },
                                    on: {
                                        click: () => {
                                            this.handleView(pic)
                                        }
                                    }
                            })
                        }
                    },
                    {
                        title: '学生证背面照',
                        key: 'back_url',
                        render: (h, params) => {
                           var pic = params.row.back_url;
                           if (!pic) {
                               return h('div', 
                                    '尚未上传'
                               );
                           }
                            return h("img",{
                                    attrs: {
                                        src: pic,
                                    },
                                    style:{
                                        width:"px",
                                        height:"45px",
                                    },
                                    on: {
                                        click: () => {
                                            this.handleView(pic)
                                        }
                                    }
                            })
                        }
                    },
                    
                    {
                        title: '发布时间',
                        key: 'time',
                        render: function (h, params) {
                            return h('div', 
                               new Date(this.row.time*1000).Format('yyyy-MM-dd hh:mm')
                            );
                        }
                    },
                    {
                        title: '状态',
                        key: 'status',
                        render: function (h, params) {
                            var statusValue = ['未认证','认证通过', '认证驳回', '待审核']
                            return h('div', 
                              statusValue[params.row.status]
                            );
                        }
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            if (params.row.status == 0 || params.row.status == 3) {
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.f(params.index, 2)
                                            }
                                        }
                                    }, '驳回'),
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.f(params.index, 1)
                                            }
                                        }
                                    }, '通过'),
                                ]);
                            } 
                                
                            return h('p',{
                                style: {
                                    color: 'red'
                                },},'已审核');
                            
                        }
                    }
                ];
          
        },
        data () {
            return {
                columns: [],
                data: [],
                paging: {},
                visible: false,
                imageSrc: ''
            }
        },
        methods: {
            fRadio() {
               this.paging.page = 1;
               this.load_data(this.paging);
            },
            load_data(paging) {
                this.$http.post(util.baseURL+"student/studentlist",paging,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                              this.data = response.data.data.list;
                              this.paging.total = response.data.data.total;
                            } else {
                               this.$Message.error("暂无申请列表！");
                            }
                        },
                        (error)=>{
                            console.log(error);
                        }
                );
            },
            f (index, status) {
                var body = {
                    id : this.data[index].id,
                    status : status,
                };
                //修改状态
                 this.$http.post(util.baseURL+"student/updatestudent",body,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                              this.data[index].status = status;
                            } else {
                               this.$Message.error("修改失败！");
                            }
                        },
                        (error)=>{
                            console.log(error);
                        }
                );
            },
            changepage(page) {
                this.paging.page = page;
                this.load_data(this.paging);
            },
            // 图片显示
            handleView(imageSrc) {
                this.imageSrc = imageSrc;
                this.visible = true;
            },
            initFormatter(){
                Date.prototype.Format = function(fmt) { //
                    let o = {  
                        "M+" : this.getMonth()+1,                 //月份  
                        "d+" : this.getDate(),                    //日  
                        "h+" : this.getHours(),                   //小时  
                        "m+" : this.getMinutes(),                 //分  
                        "s+" : this.getSeconds(),                 //秒  
                        "q+" : Math.floor((this.getMonth()+3)/3), //季度  
                        "S"  : this.getMilliseconds()             //毫秒  
                    };  
                    if(/(y+)/.test(fmt))  
                        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));  
                    for(var k in o)  
                        if(new RegExp("("+ k +")").test(fmt))  
                            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));  
                    return fmt;  
                }
            }
        }
    }
</script>
